package com.magnificent.atlantis.client.robot.ui;

import com.magnificent.atlantis.client.resources.AtlantisClientResources;
import com.magnificent.atlantis.client.robot.strategies.RobotManagerStrategy;
import com.magnificent.atlantis.client.robot.ui.configure.RobotConfiguration;
import com.magnificent.atlantis.client.robot.ui.panel.AbstractUserTabPanel;
import com.magnificent.commons.utils.collections.CollectionsUtils;
import com.magnificent.commons.utils.collections.list.MetaArrayList;
import com.magnificent.commons.utils.collections.list.MetaList;
import com.magnificent.helpers.CommonUIHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

/**
 * User: Babanin
 * Date: 28.08.2009
 * Time: 1:59:48
 */
public class MainFrame extends JFrame {

    private JButton stopButton;
    private JButton startButton;

    private List<AbstractUserTabPanel> panels;
    private JTabbedPane configurationsTabbedPane;

    private MetaList<RobotConfiguration> configurations;

    public MainFrame(MetaList<RobotConfiguration> configurations) {
        this.configurations = configurations;

        initilaizeControls();
        initializeView();
    }

    private void initializeView() {
        setTitle("Atlantis Robots");
        setLayout(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(800, 700));
        setIconImage(AtlantisClientResources.ROBOT.getImage());


        add(initializeTopPanel(), BorderLayout.NORTH);
        add(CommonUIHelper.packInShiftedPanel(configurationsTabbedPane, 5, 5, 5, 5), BorderLayout.CENTER);


        pack();
        setLocationRelativeTo(null);
    }

    private JPanel initializeTopPanel() {
        JPanel topPanel = new JPanel(new BorderLayout());

        startButton = new JButton(new AbstractAction("", AtlantisClientResources.RUN) {
            public void actionPerformed(ActionEvent e) {
                for (AbstractUserTabPanel panel : panels) {
                    panel.start();
                }

                stopButton.setEnabled(true);
                startButton.setEnabled(false);
            }
        });

        stopButton = new JButton(new AbstractAction("", AtlantisClientResources.STOP) {
            public void actionPerformed(ActionEvent e) {
                for (AbstractUserTabPanel panel : panels) {
                    panel.stop();
                }

                stopButton.setEnabled(false);
            }
        });

        stopButton.setEnabled(false);

        JLabel nameLabel = new JLabel("Atlantis Robots");
        nameLabel.setFont(new Font(nameLabel.getFont().getFontName(), Font.BOLD, 16));

        JLabel descrLabel = new JLabel(" - Enterprise modelling suite");
        descrLabel.setFont(new Font(nameLabel.getFont().getFontName(), Font.PLAIN, 14));

        topPanel.add(
                CommonUIHelper.createBorderLayoutPanel(
                        CollectionsUtils.array(
                                CommonUIHelper.createHorizontalBox(5, nameLabel, 2, descrLabel),
                                CommonUIHelper.createHorizontalBox(
                                        startButton, 2, stopButton, 5
                                )
                        ),
                        CollectionsUtils.array(
                                BorderLayout.LINE_START, BorderLayout.LINE_END
                        )
                )
        );

        topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        return topPanel;
    }

    private void initilaizeControls() {
        panels = new MetaArrayList<AbstractUserTabPanel>();
        configurationsTabbedPane = new JTabbedPane();

        for (RobotConfiguration configuration : configurations) {
            AbstractUserTabPanel tabPanel = AbstractUserTabPanel.createTab(configuration);
            tabPanel.getJSplitPane().addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    int newValue = (Integer) evt.getNewValue();
                    for (AbstractUserTabPanel panel : panels) {
                        panel.getJSplitPane().setDividerLocation(newValue);
                    }
                }
            });

            ImageIcon tabIcon = configuration.getStrategy() instanceof RobotManagerStrategy ?
                    AtlantisClientResources.MANAGER : AtlantisClientResources.WORKER;
            String tabTitle = configuration.getUser().getName();

            configurationsTabbedPane.addTab(tabTitle, tabIcon, tabPanel);
            panels.add(tabPanel);
        }
    }
}
